﻿/**
* Created with JetBrains PhpStorm.
* User: vander
* Date: 3/6/13
* Time: 11:41 PM
* To change this template use File | Settings | File Templates.
*/
///For requestAnimationFrame
(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
            || window[vendors[x]+'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
                timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

document.observe("dom:loaded", function () {
    myManager = new ObjectsManager();
    myManager.start();

//    //Khoi tao menu2 tu JSON
    (function(){
        new Ajax.Request('data/myMenu2.json', {
            method: 'get',
            onSuccess: function (transport) {
                try {
                    var o = transport.responseText.evalJSON(true);
                    var menu1 = new MyMenu(o, undefined, 2);


                    if(menu1 !== undefined){
                        //$(menu1.divTag).addClassName("myMenu2");af
                        //$(menu1.divTag).setStyle({zIndex: 1});
                        var d = new webkit_draggable(menu1.id, { onStart: function(){
                            $(menu1.divTag).setStyle({backgroundColor: "#e9967a"});
                        }});

                        var d1 = new webkit_draggable(menu1.childs[0].id, {});

                        new webkit_click($(menu1.divTag), {onClick: function(){
                            $(menu1.divTag).setStyle({backgroundColor: "fuchsia", opacity: "0.5"});

                            var anim = "shake";
                            $j('#'+menu1.id).removeClass().addClass("animated "+anim);
                            var wait = window.setTimeout(function(){
                                $j('#'+menu1.id).removeClass();
                            }, 1300);

                        }});
                        myManager.add(menu1);
                    }
                }
                catch (err) {
                    alert(err);
                }
            },
            onFailure: function () {
                alert("JSON Fail");
            }
        });
    })();

//
////Khoi tao menu1 tu JSON
//    (function(){
//        new Ajax.Request('data/myMenu1.json', {
//            method: 'get',
//            onSuccess: function (transport) {
//                try {
//                    var o = transport.responseText.evalJSON(true);
//                    var menu1 = new MyMenu(o, undefined, 1);
//
//                    if(menu1 !== undefined){
//                        //$(menu1.divTag).addClassName("myMenu1");
//                        //$(menu1.divTag).setStyle({zIndex: 1});
//
//                        var d = new webkit_draggable(menu1.id, {});
//                        myManager.add(menu1);
//                    }
//                }
//                catch (err) {
//                    alert(err);
//                }
//            },
//            onFailure: function () {
//                alert("JSON Fail");
//            }
//        });
//    })();
//
//    MyTools.loadJsCssFile("css/myCSS.css", "css");

//    var hitList = new Array();
//    var isHit = false;
//
//    var touchStart = function(event){
//        event.preventDefault();
//        //event.stopPropagation();
//
//        hitList.length = 0;
//        console.log("TouchStart : " + event.targetTouches[0].pageX + "   " + event.targetTouches[0].pageY);
//        if(myManager.getControlFromTouch(event.targetTouches[0].pageX, event.targetTouches[0].pageY) !== undefined){
//            isHit = true;
//        }
//        else{
//            isHit = false;
//        }
//    };
//
//    var touchMove = function(event){
//        event.preventDefault();
//      if(!isHit){
//          var control = myManager.getControlFromTouch(event.targetTouches[0].pageX, event.targetTouches[0].pageY);
//          if(control !== undefined){
//              console.log(event.targetTouches[0].pageX + "   " + event.targetTouches[0].pageY + " --> " + control.id);
//              var evObj = document.createEvent("Events");
//              evObj.initEvent("mouseenter", true, false);
//              control.divTag.dispatchEvent(evObj);
//          }
//      }
//    };
//    var touchEnd = function(event){
//        event.preventDefault();
//    };
//
//    $(document).addEventListener("touchstart", touchStart, false);
//    $(document).addEventListener("touchmove", touchMove, false);
//    $(document).addEventListener("touchend", touchEnd, false);
//
//
//    var timer = new MyTimer({
//        id:"timer1",
//        displayOptions: {top: 400, left: 40, width:100, height: 100},
//        time: {seconds: 6}
//    }, undefined, 4);
//    timer.start();
//    myManager.add(timer);
//
//    timer.registerEvent({
//        type: "click",
//        eventHandler: function(){
//            timer.start();
//        }
//    });
for(var i = 0 ; i < 600 ; i++){
    var l = Math.floor(Math.random() * 400);
    var t = Math.floor(Math.random() * 800);
    var obj1 = new MyButton({
        id: "buttonTest" + i,
        imgSources: {
            pattern: "img/Ani/Untitled_00000",
            from: 118,
            fileType: "png",
            to: 206
        },
        displayOptions: { "left": l, "top": t, "width": 100, "height": 66 }
    });
    myManager.add(obj1);
}




//    myManager.add(obj);
//    myManager.add(obj1);

    //    this.buttons[0].registerEvent("click", function(button){
    //        $(button.divTag).addClassName("myButton1");
    //    }, this.buttons[0])

    //    var but = myMenu1.getButtonFromId("button1");
    //    but.registerEvent({
    //
    //    });

});

